$(document).ready(function() {
    $('.btn_confirm').on('click',function(e) {
  location.href = "../../../user-login.html"/*tpa=http://www.quyundong.com/order/orderPay?sn=*/ + $(".dueOrderName").attr("pay_id");
});

//点击关闭按钮的时候，遮罩层关闭
$(".shadow,.btn_close,.btn_submit,.btn_cancel,.btn_confirm").click(function () {
   $("#shadow,.alert_box,.bigMap").css("display", "none"); 
});

$('.btn_cancel').on('click',function(e) {
 var requestParam = {
    'order_id' : $('.dueOrderName').attr("order_id")
  };

  $.ajax({
    type: "get",
    url: '/order/cancelOrder?random='+ Math.random(),
    data: $.param(requestParam),
    timeout: 3000,
    success: function (data) {
      openCommonAlert("订单已取消",function(){
        location.reload();
      },"确定");
    },
    error: function () {
      openCommonAlert("失败，请稍后再试！");
    }
  });
});
});

function showUnpaidOrderView(){
    $("#shadow").css({
        display: "block", height: $(document).height()
    });
    $('#unpaid_alert_box .title').text("你有尚未支付订单");

    var $box = $('#unpaid_alert_box');
    $box.css({
        display: "block"
    });
    
   var height = $(".display-unpaid-holder").height();
    $(".dueOrderList .court").height(height*$(".display-unpaid-holder").length);
}

function buildAndShowUnpaidOrderView(order){
  var titleStr = '<dd><strong order_id="{0}" pay_id="{1}" class="dueOrderName">{2}</strong></dd>';
  var courtOrderStr ='<dt>日期：</dt><dd><strong>{0}</strong></dd><dt class="court" style="height: 90px;">场次：</dt>';
  var goodsOrderStr ='<dt>套餐：</dt><dd><strong>{0}</strong></dd><dt>数量：</dt><dd><strong>{1}</strong></dd>';
  var orderStr = '<dd class="display-unpaid-holder">{0}</dd>';
  
  $(".dueOrderList").empty();
  if (order.order_type == "0") {
    $(".dueOrderList").append(titleStr.format(order.order_id,order.pay_order_id,order.name)+courtOrderStr.format(order.book_date));
    jQuery.each(order.goods_list,function(i,goods){
    var startTime = transTimeFormart(goods.start_time);
    var endTime = transTimeFormart(goods.end_time);
    var html = orderStr.format(startTime+'-'+endTime+' '+goods.course_name+' '+goods.shop_price+'元');
    $(".dueOrderList .court").after(html);

  });
  } else if (order.order_type == "1"){
     $(".dueOrderList").append(titleStr.format(order.order_id,order.pay_order_id,order.name)+goodsOrderStr.format(order.goods_list[0].goods_name,order.goods_list[0].goods_number));
  };

  $($(".display-unpaid-holder")[order.goods_list.length-1]).after('<dt>共计:</dt><dd><strong>'+ order.order_amount +'元</strong></dd>'); 

  showUnpaidOrderView();
}

function checkOrderDueCount(callback,param){
  $.ajax({
    type: "get",
    url: '/order/getOrderDueCount?random='+ Math.random(),
    timeout: 3000,
    success: function(data) {
      if(data.status == "0000"){
        var data = data.data;
        if (data.count == "0") {
          $(".dueOrderList").empty();
          if(callback != null){
            callback(param); 
          }      
        } else {
          buildAndShowUnpaidOrderView(data.order);
        }
      } else if(data.status == "0202"){
          openLoginDiglog(refreshPage);
      }
    },
    error: function () {
      openCommonAlert("失败，请稍后再试！");
    }
    });
}

